package org.jiamandan.com.strategy.impl.car.cmd;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.YxConstant;
import org.jiamandan.com.dto.ApplyRechargeDto;
import org.jiamandan.com.strategy.ICarCmdBaseService;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.YxObjectUtil;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Lee
 * @email:honvy.lee@qq.com
 * @see:
 * @创建日期：2023-03-30 11:00
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2023-03-30 11:00	         Lee        	创建
 * @end
 */
@Service
@AllArgsConstructor
@Slf4j
public class YxApplyRechargeService implements ICarCmdBaseService {

    public void doExecute(byte[] data, Channel client) {
        ApplyRechargeDto dto = JSON.parseObject(CommonUtil.bytesToString(data), ApplyRechargeDto.class);
        StringBuilder builder = new StringBuilder();
        String pileNo = StringUtils.leftPad(CommonUtil.toHexString(dto.getPileNo()), 14, '0');
        builder.append(pileNo);
        builder.append(StringUtils.leftPad(CommonUtil.toHexString(dto.getGunNo()), 2, '0'));
        builder.append(StringUtils.leftPad(CommonUtil.toHexString(dto.getStartMethod()), 2, '0'));
        builder.append(StringUtils.leftPad(CommonUtil.toHexString(dto.getIsNeedPwd()), 2, '0'));
        builder.append(StringUtils.leftPad(CommonUtil.toHexString(dto.getAccount()), 16, '0'));
        builder.append(StringUtils.leftPad(CommonUtil.toHexString(dto.getPassword()), 32, '0'));
        builder.append(StringUtils.leftPad(CommonUtil.toHexString(dto.getVin()), 34, '0'));

        String replyMsg = YxObjectUtil.pushMessage(builder.toString(),  YxConstant.Cmd.CMD_31);
        log.info("充电桩主动申请启动充电(CMD = 31)：" + replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = client.writeAndFlush(buf);
    }
}
